package com.yzt.zhuju;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.yzt.zhuju.IService.message.IMessageService;
import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.network.entity.login.countryCode.GetZoneResponse;
import com.yzt.zhuju.network.entity.user.PersonalDetail;
import com.yzt.zhuju.network.entity.user.PersonalDetailBasic;
import com.yzt.zhuju.network.entity.user.UserLoginInfo;
import com.yzt.zhuju.utils.DeviceUtils;
import com.yzt.zhuju.constants.PreferenceConstant;
import com.yzt.p_base.utils.SharedPreferenceUtil;
import com.yzt.plugin_imageloader.ImageLoadUtil;

public class RXingUtils {

    public static final String LOGIN_REGISTER = "LOGIN_REGISTER";
    private static final String GENDER = "GENDER";
    private static final String PERSONINFO_DETAIL = "PERSONINFO_DETAIL";
    private static final String LOGIN_ACCOUNT = "LOGIN_ACCOUNT";
    private static final String LOGIN_TOKEN = "LOGIN_TOKEN";
    private static final String COUNTRY_AND_ZONE = "COUNTRY_AND_ZONE";
    private static final String KEY_REMAIN_DIAMOND = "remain_diamond";
    private static final String DEVICE_ID = "device_id";
    //聊天室不显示头饰
    private static final String NO_CHATROOM_FRAME = "NO_CHATROOM_FRAME";

    /**
     * 获取是否显示聊天室头饰
     */
    public static boolean getShowChatroomFrame() {
        return SharedPreferenceUtil.getInstance().getValue(NO_CHATROOM_FRAME, true);
    }

    public static void saveCountryAndZone(GetZoneResponse zone) {
        SharedPreferenceUtil.getInstance().putSystemValue(COUNTRY_AND_ZONE, JSON.toJSONString(zone));
    }

    public static GetZoneResponse getCountryAndZone() {
        String data = (String) SharedPreferenceUtil.getInstance().getSystemValue(COUNTRY_AND_ZONE, "");
        if (TextUtils.isEmpty(data)) {
            return null;
        }
        GetZoneResponse mGetZoneResponse = JSON.parseObject(data, GetZoneResponse.class);
        return mGetZoneResponse;
    }

    public static boolean isLogin() {
        return !TextUtils.isEmpty(getLoginToken());
    }

    public static boolean isShowNewPerfectDialog;//是否弹出完善资料弹窗

    public static int getGender() {
        return SharedPreferenceUtil.getInstance().getValue(GENDER, 0);
    }

    public static void setGender(int gender) {
        if (gender == 0) {
            return;
        }
        SharedPreferenceUtil.getInstance().putValue(GENDER, gender);
    }

    public static void logout() {
        SharedPreferenceUtil.getInstance().putValue(GENDER, 0);
        saveLoginAccount(null);
        saveLoginToken("");
    }

    public static void saveLoginAccount(UserLoginInfo loginAccount) {
        if (loginAccount == null || loginAccount.getUserInfo() == null) {
            SharedPreferenceUtil.getInstance().putValue(LOGIN_ACCOUNT, "");
            saveLoginToken("");
        } else {
//            //绑定push别名
            SharedPreferenceUtil.getInstance().putValue(LOGIN_REGISTER, loginAccount.isAuthed());
            SharedPreferenceUtil.getInstance().putValue(LOGIN_ACCOUNT, JSON.toJSONString(loginAccount));
            SharedPreferenceUtil.getInstance().putValue(PreferenceConstant.LOGIN_MOBILE, getLoginAccount().getUserInfo() == null ? null : getLoginAccount().getUserInfo().getMobile());
            saveLoginToken(loginAccount.getToken());
        }
    }

    public static String getLoginMobile() {
        return SharedPreferenceUtil.getInstance().getValue(PreferenceConstant.LOGIN_MOBILE, getLoginAccount().getUserInfo() == null ? null : getLoginAccount().getUserInfo().getMobile());
    }

    public static UserLoginInfo getLoginAccount() {
        UserLoginInfo mUserData = new UserLoginInfo();
        String data = SharedPreferenceUtil.getInstance().getValue(LOGIN_ACCOUNT, "");
        if (TextUtils.isEmpty(data)) {
            return mUserData;
        }
        mUserData = JSON.parseObject(data, UserLoginInfo.class);
        return mUserData;
    }

    /**
     * 更新用户信息  刷新IM基础资料
     */
    public static void savePersonDetail(PersonalDetailBasic mPersonalDetail) {

        PersonalDetail personalDetail = new PersonalDetail();
        personalDetail.setUserBasic(mPersonalDetail);

        SharedPreferenceUtil.getInstance().putValue(PERSONINFO_DETAIL, personalDetail == null ? "" : JSON.toJSONString(personalDetail));
        if (personalDetail == null) {
            return;
        }
        PersonalDetailBasic mBasicInfo = mPersonalDetail;
        if (mBasicInfo != null) {
            //更新IM模块用户信息
            IMessageService messageService = ARouter.getInstance().navigation(IMessageService.class);
            String extraStr = null;
            messageService.updateUserInfo(
                    mBasicInfo.getUserId(),
                    mBasicInfo.getNickName(),
                    ImageLoadUtil.url2webp(mBasicInfo.getAvatar(), 150),
                    extraStr);
        }
    }

    public static PersonalDetail getPersonDetail() {
        PersonalDetail mPersonalDetail = null;
        String data = SharedPreferenceUtil.getInstance().getValue(PERSONINFO_DETAIL, "");
        if (TextUtils.isEmpty(data)) {
            return mPersonalDetail;
        }
        mPersonalDetail = JSON.parseObject(data, PersonalDetail.class);
        return mPersonalDetail;
    }

    public static String getUserId() {
        String data = SharedPreferenceUtil.getInstance().getValue(LOGIN_ACCOUNT, "");
        if (TextUtils.isEmpty(data)) {
            return null;
        }
        UserLoginInfo mPersonalDetail = JSON.parseObject(data, UserLoginInfo.class);
        return mPersonalDetail.getUserInfo().getUid().toString();
    }

    public static void saveLoginToken(String token) {
        SharedPreferenceUtil.getInstance().putValue(LOGIN_TOKEN, token);
    }

    public static String getLoginToken() {
        return SharedPreferenceUtil.getInstance().getValue(LOGIN_TOKEN, "");
    }

    public static void saveDeviceId(String deviceID) {
        SharedPreferenceUtil.getInstance().putValue(DEVICE_ID, deviceID);
    }

    public static String getDeviceId() {
        return SharedPreferenceUtil.getInstance().getValue(DEVICE_ID, DeviceUtils.getAndroidID(AppConfig.context));
    }

    /**
     * 默认的国家区号
     */
    public static String getDefaultZonde() {
        return "86";
    }

    public static void saveRemainDiamond(float remainDiamond) {
        SharedPreferenceUtil.getInstance().putValue(KEY_REMAIN_DIAMOND, remainDiamond);
    }

    public static float getRemainDiamond() {
        return SharedPreferenceUtil.getInstance().getValue(KEY_REMAIN_DIAMOND, 0f);
    }
}
